package com.example.chapter9file;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class MainActivity extends AppCompatActivity {
    private Button btnSaveFile,btnReadContent;
    private TextView tvResult;
    private EditText content,etFilename;
    String filename;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnReadContent = findViewById(R.id.btn_read);
        btnSaveFile = findViewById(R.id.btn_savefile);
        tvResult = findViewById(R.id.tv_result);
        content = findViewById(R.id.etm_content);
        etFilename = findViewById(R.id.et_filename);
        btnReadContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                filename = etFilename.getText().toString()+".txt";
                try {
                    FileInputStream fileInputStream = openFileInput(filename);
                    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
                    char[] chars = new char[fileInputStream.available()];
                    inputStreamReader.read(chars);
                    inputStreamReader.close();
                    fileInputStream.close();
                    String s = new String(chars);
                    tvResult.setText("文件内容"+s);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        btnSaveFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvResult.setText("");
                filename = etFilename.getText().toString()+".txt";
                Log.d("TAG", "文件名: "+filename);
                try {
                    FileOutputStream fileOutputStream = openFileOutput(filename, Context.MODE_PRIVATE);
                    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
                    outputStreamWriter.write(content.getText().toString());
                    outputStreamWriter.flush();
                    fileOutputStream.flush();
                    outputStreamWriter.close();
                    fileOutputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
                Toast.makeText(MainActivity.this,"写入完成",Toast.LENGTH_LONG).show();
            }
        });
    }
}